﻿using Microsoft.Reporting.WinForms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NoDriverPrintHelperV1d0.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using TmU220PrintHelperV2d0.Entity;
using TmU220PrintHelperV2d0.HttpHelper;
using TmU220PrintHelperV2d0.Service;
using TmU220PrintHelperV2d0.Singleton;

namespace FunctionTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public object str { get; private set; }

        private void button1_Click(object sender, EventArgs e)
        {
            //得到项目主Url
            string url = ApiInterfaceInfoSingletonV3d0.Instance.GetProjectMainUrl();
            //构建post数据
            string postData = HotchPotchV2d0.GetPostDataOfGetTableData("rms_item");
            //同步方式调用接口
            string returnString = HttpFunctionV2d0.RunApiInterfacePost(url, postData);
            //解析接口返回json串
            dynamic rootDynamicObject = JsonConvert.DeserializeObject<dynamic>(returnString);
            dynamic statusDynamicObject = rootDynamicObject.status;
            dynamic contentDynamicObject = rootDynamicObject.content;
            dynamic struDynamicObject = rootDynamicObject.stru;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //得到项目主Url
            string url = ApiInterfaceInfoSingletonV3d0.Instance.GetProjectMainUrl();
            //构建post数据
            string tableName = "rms_printer";
            string postData = HotchPotchV2d0.GetPostDataOfGetTableData(tableName);
            //同步方式调用接口
            string returnString = HttpFunctionV2d0.RunApiInterfacePost(url, postData);
            //解析接口返回json串
            dynamic rootDynamicObject = JsonConvert.DeserializeObject<dynamic>(returnString);
            dynamic statusDynamicObject = rootDynamicObject.status;
            dynamic contentDynamicObject = rootDynamicObject.content;
            dynamic struDynamicObject = rootDynamicObject.stru;

            List<string> fieldNameList = new List<string>();
            //dynamic dModel = new DynamicModel();
            foreach (dynamic item in struDynamicObject)
            {
                fieldNameList.Add(item["COLUMN_NAME"].Value);
            }
            foreach(dynamic item in contentDynamicObject)
            {
                List<string> valueList = new List<string>();
                rms_printer pInfo = new rms_printer();
                foreach (dynamic fName in item)
                {
                    string tt = fName.Name.ToString();                      ;
                    string ty = fName.Value.ToString();
                    HotchPotchV2d0.SetObjectPropertyValue(tt, ty, pInfo);
                    //var ss = item.id;
                    //string ss = item[fName].Value;//.ToString();
                    //valueList.Add(item[fName].ToString());
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var xyz = PrinterInfoSetSingletonV2d0.Instance.GetPrinterInfoSet();
            //var ttt = OrderPrintMainAndDetailSingletonV3d0.Instance.GetPrintMainList();
            //var tty = OrderPrintMainAndDetailSingletonV3d0.Instance.GetPrintDetailList();
            //var xyz= LoadDataByApiInterface<rms_printer>();
        }
        /// <summary>
        /// 装载数据
        /// </summary>
        private void LoadData()
        {
            //得到项目主Url
            string url = ApiInterfaceInfoSingletonV3d0.Instance.GetProjectMainUrl();
            //构建post数据
            string tableName = "rms_printer";

            Type typeOfTableClass = typeof(rms_printer);

            string postData = HotchPotchV2d0.GetPostDataOfGetTableData(tableName);
            //同步方式调用接口
            string returnString = HttpFunctionV2d0.RunApiInterfacePost(url, postData);
            //解析接口返回json串
            dynamic rootDynamicObject = JsonConvert.DeserializeObject<dynamic>(returnString);
            dynamic statusDynamicObject = rootDynamicObject.status;
            if (rootDynamicObject.status.Value.ToString() == "00")
            {
                string contentString = rootDynamicObject.content.ToString();
                var rmsP = JsonConvert.DeserializeObject<List<rms_printer>>(contentString);

                //_printerInfoSet.PrinterInfoList = contentDynamicObject as List<PrinterInfoV3d0>;
                //dynamic struDynamicObject = rootDynamicObject.stru;
            }
        }
        private static List<T> LoadDataByApiInterface<T>()
        {
            List<T> resultList = null;
            //得到项目主Url
            string url = ApiInterfaceInfoSingletonV3d0.Instance.GetProjectMainUrl();
            //根据类，得到类名称，类名称默认就是对应数据表的表名称
            Type currType = typeof(T);
            string currClassName = currType.Name;
            //表名称
            string tableName = currClassName;
            //构建post数据
            string postData = HotchPotchV2d0.GetPostDataOfGetTableData(tableName);
            //同步方式调用接口
            string returnString = HttpFunctionV2d0.RunApiInterfacePost(url, postData);
            //解析接口返回json串
            dynamic rootDynamicObject = JsonConvert.DeserializeObject<dynamic>(returnString);
            dynamic statusDynamicObject = rootDynamicObject.status;
            if (rootDynamicObject.status.Value.ToString() == "00")
            {
                string contentString = rootDynamicObject.content.ToString();
                resultList = JsonConvert.DeserializeObject<List<T>>(contentString);
            }            
            return resultList;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string tableName = "rms_order_print_main";
            var data  = HotchPotchV2d0.LoadSpecialDataOfTableByApiInterface<rms_order_print_main>("status", "0");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var data = HotchPotchV2d0.SqlLoadDataOfTableByApiInterface<rms_order_print_detail>("select * from rms_order_print_detail");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //var jsonObject=new JObject{{"status", "1"}};
            try
            {
                HotchPotchV2d0.UpdatePrintMainTableWhenTaskPrintComplete(1, "good");
            }
            catch(Exception ex)
            {

            }
            //var result = HotchPotchV2d0.UpdateTableByApiInterface("rms_order_print_main", "id", "1", jsonObject.ToString());
            //string jsl=JObject
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var result = HotchPotchV2d0.UpdatePrinterTable(1, 3);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.reportViewer1.RefreshReport();
            this.reportViewer1.RefreshReport();
            this.reportViewer1.RefreshReport();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            var logList = DatabaseHelperV3d0.GetPrintLog(DateTime.Now,DateTime.Now.AddDays(1));
            //SetReport2();
            //var log = new PrintLogV3d0();
            //log.uid = "_5FP0L50A0";
            //log.status = 1;
            //log.DetailList.Add("打印行1");
            //log.DetailList.Add("打印行2");
            ////log.DetailList.Add("打印行3");
            ////log.DetailList.Add("打印行4");
            //log.PrintProcessDescriptionList.Add("ABC");
            //log.PrintProcessDescriptionList.Add("CDE");
            //log.PrintProcessDescriptionList.Add("EDF");
            ////log.PrintProcessDescriptionList.Add("操作4");
            ////log.PrintProcessDescriptionList.Add("操作5");

            //bool result;
            //result= HotchPotchV2d0.InsertSinglePrintLogToTable(log);
            //result = HotchPotchV2d0.InsertPrinterTable();// InsertPrintLogTable(log);
        }
        /// <summary>
        /// 设置报表
        /// </summary>
        private void SetReport()
        {
            //第一步：清除之前的数据
            this.reportViewer1.LocalReport.DataSources.Clear();
            //第二步：指定报表路径
            this.reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
            //第三步：构造新的DataTable
            DataTable dt = new DataTable("DataTable1");
            dt.Columns.Add("qty");
            dt.Columns.Add("cart_name");
            dt.Rows.Add(new object[] { "1", "ready" });
            dt.Rows.Add(new object[] { "2", "刀削面" });
            dt.Rows.Add(new object[] { "3", "蛋炒饭" });
            //名称不能写错，和报表中的数据集名称一致
            ReportDataSource rdsItem = new ReportDataSource("DataSet1", dt);
            //此处可以有多个数据源
            this.reportViewer1.LocalReport.DataSources.Add(rdsItem);
            //第四步：构造参数
            //List<ReportParameter> lstParameter = new List<ReportParameter>() {
            //    new ReportParameter("Title",this.txtTitle.Text),
            //    new ReportParameter("Id",this.txtId.Text),
            //    new ReportParameter("Name",this.txtName.Text),
            //    new ReportParameter("Age",this.txtAge.Text),
            //    new ReportParameter("Sex",this.txtSex.Text),
            //    new ReportParameter("Salary",this.txtSalary.Text),
            //    new ReportParameter("Depart",this.txtDepart.Text)
            //};
            //this.reportViewer1.LocalReport.SetParameters(lstParameter);
            this.reportViewer1.ZoomMode = ZoomMode.Percent;
            this.reportViewer1.ZoomPercent = 100;
            //第五步：刷新报表
            this.reportViewer1.RefreshReport();
        }
        private void SetReport2()
        {
            //第一步：清除之前的数据
            this.reportViewer1.LocalReport.DataSources.Clear();
            //第二步：指定报表路径
            this.reportViewer1.LocalReport.ReportPath = "Report2.rdlc";

            //List<rms_printer> printerList = new List<rms_printer>();
            //printerList.Add(new rms_printer());
            //printerList.Add(new rms_printer());
            //printerList.Add(new rms_printer());
            //printerList.Add(new rms_printer());
            var infoList = PrinterInfoSetSingletonV2d0.Instance.GetPrinterInfoSet().PrinterInfoList;

            rms_printer printer = new rms_printer();
            printer.printername = "KIT";
            infoList.Add(printer);
            printer = new rms_printer();
            printer.printername = "KIT";
            infoList.Add(printer);
            printer = new rms_printer();
            printer.printername = "BAR";
            infoList.Add(printer);
            printer = new rms_printer();
            printer.printername = "BAR";
            infoList.Add(printer);


            rms_printerBindingSource.DataSource = infoList;

            //此处可以有多个数据源
           this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetPrinterInfo", rms_printerBindingSource));
            string dir = Application.StartupPath;

            string path = "file:///"+System.AppDomain.CurrentDomain.BaseDirectory+ @"parameter\test.bmp";   //图片地址
            ReportParameter image = new ReportParameter("image", path);   //image1必须和报表参数一致
            //第四步：构造参数
            //List<ReportParameter> lstParameter = new List<ReportParameter>();
            //lstParameter.Add(image);
            this.reportViewer1.LocalReport.EnableExternalImages = true;
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { image });
            this.reportViewer1.ZoomMode = ZoomMode.Percent;
            this.reportViewer1.ZoomPercent = 100;
            //第五步：刷新报表
            this.reportViewer1.RefreshReport();
        }
    }
}
